package com.global.emc.core.components.token.impl;

import com.global.emc.core.components.token.Token;
import com.global.emc.core.components.token.TokenManager;
import com.global.emc.core.exception.impl.TokenValidationEMCException;
import com.global.emc.core.exception.impl.ValidationEMCException;

/**   
 *  
 * @Description:  [token 管理，验证动态密码，验证码、防重复提交等]   
 * @Author:       [Joly_Huang]   
 * @CreateDate:   [2014年1月30日 下午1:18:03]   
 * @UpdateUser:   [Joly_Huang]   
 * @UpdateDate:   [2014年1月30日 下午1:18:03]   
 * @UpdateRemark: [说明本次修改内容]  
 * @Version:      [v1.0] 
 *    
 */
public class RandomNumberTokenManager implements TokenManager {
	
	private String tokenName;
	private int tokenLength;
	private boolean userCapital;
	private boolean ignoreCase;
	private int delayTime;
	
	private String unavailable;
	private String tokenVerifyFailure;
	private String tokeTimeout;
	
	
	public String getUnavailable() {
		return unavailable;
	}
	public void setUnavailable(String unavailable) {
		this.unavailable = unavailable;
	}
	public String getTokenName() {
		return tokenName;
	}
	public void setTokenName(String tokenName) {
		this.tokenName = tokenName;
	}
	public int getTokenLength() {
		return tokenLength;
	}
	public void setTokenLength(int tokenLength) {
		this.tokenLength = tokenLength;
	}
	public boolean isUserCapital() {
		return userCapital;
	}
	public void setUserCapital(boolean userCapital) {
		this.userCapital = userCapital;
	}
	public boolean isIgnoreCase() {
		return ignoreCase;
	}
	public void setIgnoreCase(boolean ignoreCase) {
		this.ignoreCase = ignoreCase;
	}
	public int getDelayTime() {
		return delayTime;
	}
	public void setDelayTime(int delayTime) {
		this.delayTime = delayTime;
	}
	
	
	public String getTokenVerifyFailure() {
		return tokenVerifyFailure;
	}
	public void setTokenVerifyFailure(String tokenVerifyFailure) {
		this.tokenVerifyFailure = tokenVerifyFailure;
	}
	public String getTokeTimeout() {
		return tokeTimeout;
	}
	public void setTokeTimeout(String tokeTimeout) {
		this.tokeTimeout = tokeTimeout;
	}
	
	
	@Override
	public Token createToken() {
		return new RandomNumberToken(tokenLength, userCapital);
	}

	@Override
	public void verifyToken(String tokenUniqueId, Token cacheToken)throws ValidationEMCException {
		
		Token token = getCacheToken(tokenUniqueId, cacheToken);
		
		if(null == token)
			throw new TokenValidationEMCException(tokenVerifyFailure);
			
		
		if(!verifyTokenDelayTime(token))
			throw new TokenValidationEMCException(tokeTimeout);
		
		synchronized(cacheToken){
			if(cacheToken.isUnavailable())
				throw new TokenValidationEMCException(unavailable);
				
			cacheToken.setUnavailable(true);
		}
		
	}
	
	protected boolean verifyTokenDelayTime(Token currentToken){
		long currentDelayTime = System.currentTimeMillis()-currentToken.getAccessDate();
		
		if(currentDelayTime > (delayTime*1000))
			return false;
		
		return true;
	}
	
	protected Token getCacheToken(String tokenUniqueId, Token cacheToken){
		
		if(ignoreCase){
			if(tokenUniqueId.toLowerCase().equals(cacheToken.getUniqueId().toLowerCase()))
				return cacheToken;
		}else{
			if(tokenUniqueId.equals(cacheToken.getUniqueId()))
				return cacheToken;
		}
		
		return null;
		
	}

}
